﻿using System.Collections.Generic;
using System.Linq;
using AutoMapper;

namespace AndrewSiemer.KnowledgeExchange.DataAccess
{
    public class PostRepository : IPostRepository
    {
        private IConnection _connection;

        public PostRepository(IConnection connection)
        {
            _connection = connection;
        }

        public List<Domain.Post> GetAllPosts()
        {
            List<Post> result = new List<Post>();
            using(KEDataContext dc = _connection.GetContext())
            {
                result = dc.Posts.OrderByDescending(p => p.CreateDate).ToList();
            }

            return Mapper.Map<List<DataAccess.Post>, List<Domain.Post>>(result);
        }
    }
}